import { defineStore } from "pinia";
import axios from "axios";
import { nanoid } from "nanoid";
/* export const useTalkStore = defineStore('talk', {
  actions: {
    async getATalk() {
      // 发请求，下面这行写法是：连续结构复制+重命名
      let { data: {content:title} } = await axios.get('https://api.uomg.com/api/rand.qinghua?fromat=json')
      // 把请求回来的字符串，包装成一个对象。
      let obj = {id: nanoid(), title}
      this.talkList.unshift(obj)
    }
  },
  // 真正存储数据的地方
  state() {
    return {
      talkList: JSON.parse(localStorage.getItem('talkList') as string ) || []
    }
  },
}) */

import { ref, reactive } from "vue";
export const useTalkStore = defineStore('talk', () => {
  // talkList 就是 state
  const talkList = reactive(JSON.parse(localStorage.getItem('talkList') as string ) || [])

  // getTalk 函数相当于 action
  async function getATalk() {
    // 发请求，下面这行写法是：连续结构复制+重命名
    let { data: { content: title } } = await axios.get('https://api.uomg.com/api/rand.qinghua?fromat=json')
    // 把请求回来的字符串，包装成一个对象。
    let obj = { id: nanoid(), title }
    talkList.unshift(obj)
  }
  return {talkList, getATalk}
})
